<?php

namespace app\api\controller\pick;

use app\common\controller\Api;
use app\common\model\Order as ModelOrder;
use app\common\model\Shop;
use app\common\model\User;

/**
 * 服务点订单接口
 */
class Order extends Api
{

    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = [];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    /**
     * 列表
     *
     */
    public function list()
    {
        $get =  request()->get();
        extract($get);
        if (!isset($type) || empty($type)) {
            $this->error('参数错误');
        }
        $userInfo = User::get($this->auth->id);
        $shop_ids = Shop::where('city', $userInfo['city'])->where('status', 'normal')->column('id');
        $order = new ModelOrder();
        $list = $order->with(['goods', 'address', 'shop',  'pickInfo','cleanInfo','retrieveInfo'])->where('order.status', $type)->whereIn('order.shop_id', $shop_ids)->order('createtime desc')->paginate(10);
        $this->success('返回成功', $list);
    }


    /**
     * 确认取货
     *
     */
    public function confirm_picking()
    {
        $post =  request()->post();
        extract($post);
        if (!isset($id) || empty($id)) {
            $this->error('参数错误');
        }
        $order = new ModelOrder();
        $order = $order->get($id);
        if ($order['status'] != 2) {
            $this->error('订单状态错误');
        }
        $res = $order->save(['status' => '3', 'pick' => $this->auth->id, 'picktime' => time()]);
        if ($res) {
            $this->success('取货成功');
        }
        $this->error('取货失败');
    }
    /**
     * 扫码取货
     *
     */
    public function sao_picking()
    {
        $post =  request()->post();
        extract($post);
        if (!isset($id) || empty($id)) {
            $this->error('参数错误');
        }
        $order = new ModelOrder();
        $order = $order->get($id);
        if ($order['status'] != 5) {
            $this->error('订单状态错误');
        }
        $res = $order->save(['status' => '6','retrieve' => $this->auth->id]);
        if ($res) {
            $this->success('提交成功');
        }
        $this->error('提交失败');
    }
    /**
     * 取货员数据统计
     *
     */
    public function sao_pickings()
    {
        $today_num = ModelOrder::where('pick', $this->auth->id)->whereTime('taketime', 'today')->where('status', '<>', '-1')->count();
        $data['money_revenue']  = bcmul($today_num, config('site.service_commission'), 2);
        $month_num = ModelOrder::where('pick', $this->auth->id)->whereTime('taketime', 'month')->where('status', '<>', '-1')->count();
        $data['month_revenue']  = bcmul($month_num, config('site.picker_commission'), 2);
        $data['today_order_num']  = ModelOrder::where('pick', $this->auth->id)->whereTime('picktime', 'today')->where('status', '<>', '-1')->count();
        $data['order_num']  = ModelOrder::where('pick', $this->auth->id)->whereTime('picktime', 'today')->where('status', '<>', '-1')->count();
        $this->success('', $data);
    }

    /**
     * 订单地图
     *
     */
    public function order_map()
    {
        $userInfo = User::get($this->auth->id);
        $shops = Shop::where('city', $userInfo['city'])->where('status', 'normal')->select();
        foreach($shops as $k=>$v){
            if($v['order_num']==0){
                unset($shops[$k]);
            }
        }
        $this->success('', $shops);
    }
}
